package com.hymall.service.wx;

import com.hymall.bean.MarketUser;
import com.hymall.bean.MarketUserExample;
import com.hymall.bean.WxRegisterCode;
import com.hymall.bean.WxRegisterCodeExample;
import com.hymall.bean.bo.WxRegisterBo;
import com.hymall.mapper.MarketUserMapper;
import com.hymall.mapper.WxRegisterCodeMapper;
import com.hymall.util.DateUtils;
import com.hymall.util.Md5Util;
import com.sun.org.apache.bcel.internal.generic.MONITORENTER;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author: 叶熠彬
 * @description: 微信注册用户（验证码写入、比对）
 * @date: 2022/7/20 12:36
 */
@Service
public class WxAuthServiceImpl implements WxAuthService {

    @Autowired
    WxRegisterCodeMapper codeMapper;

    @Autowired
    MarketUserMapper userMapper;

    /**
     * 是否在一分钟内获取过验证码
     * 如果没有，记录发送的验证码
     *
     * @author: 叶熠彬
     * @date: 2022/7/20 16:45
     * @param: [mobile, code]
     * @return: int
     */
    @Override
    public int addCodeByPhone(String mobile, String code) {
        WxRegisterCodeExample example = new WxRegisterCodeExample();
        example.createCriteria().andPhoneEqualTo(mobile);
        WxRegisterCode wxRegisterCode = new WxRegisterCode();
        wxRegisterCode.setCode(code);
        wxRegisterCode.setDeleted(false);
        //五分钟内有效
        wxRegisterCode.setAddTime(new Date());

        List<WxRegisterCode> wxRegisterCodes = codeMapper.selectByExample(example);
        //没发送过验证码，插入
        if (wxRegisterCodes.size() == 0) {
            wxRegisterCode.setPhone(mobile);
            codeMapper.insertSelective(wxRegisterCode);
        } else {
            codeMapper.updateByExampleSelective(wxRegisterCode, example);
        }

        return 0;
    }

    /**
     * 是否在一分钟内获取过验证码
     *
     * @author: 叶熠彬
     * @date: 2022/7/20 16:47
     * @param: [mobile]
     * @return: java.util.Date
     */
    @Override
    public Date queryLastGetCodeTime(String mobile) {
        WxRegisterCodeExample example = new WxRegisterCodeExample();
        example.createCriteria().andPhoneEqualTo(mobile);
        List<WxRegisterCode> wxRegisterCodes = codeMapper.selectByExample(example);
        if (wxRegisterCodes.size() == 0) {
            return null;
        }
        Date addTime = wxRegisterCodes.get(0).getAddTime();
        return addTime;
    }

    /**
     * 注册成功后，物理删除code表中的mobile对应信息
     * 判断优先级：704用户名重复 > 705手机号已注册 > 703验证码错误
     *
     * @author: 叶熠彬
     * @date: 2022/7/20 16:47
     * @param: [bo]
     * @return: int
     */
    @Override
    public int addWxUser(WxRegisterBo bo) {
        String username = bo.getUsername();
        String mobile = bo.getMobile();
        String code = bo.getCode();
        //用户名判断
        MarketUserExample example1 = new MarketUserExample();
        example1.createCriteria().andUsernameEqualTo(username);
        List<MarketUser> marketUsers1 = userMapper.selectByExample(example1);
        if (marketUsers1.size() != 0) {
            return 704;
        }
        //手机号判断
        MarketUserExample example2 = new MarketUserExample();
        example2.createCriteria().andMobileEqualTo(mobile);
        List<MarketUser> marketUsers2 = userMapper.selectByExample(example2);
        if (marketUsers2.size() != 0) {
            return 705;
        }
        //验证码判断
        WxRegisterCodeExample codeExample = new WxRegisterCodeExample();
        codeExample.createCriteria().andPhoneEqualTo(mobile);
        List<WxRegisterCode> wxRegisterCodes = codeMapper.selectByExample(codeExample);
        WxRegisterCode wxRegisterCode = wxRegisterCodes.get(0);
        Date lastAddTime = wxRegisterCode.getAddTime();
        //超过五分钟，按错误处理
        Date date = new Date();
        if (DateUtils.addMinutes(lastAddTime, 5).before(date)) {
            return 703;
        }
        //验证码是否匹配
        String correctCode = wxRegisterCode.getCode();
        if (!code.equals(correctCode)) {
            //验证码错误
            return 703;
        }
        MarketUser user = new MarketUser();
        BeanUtils.copyProperties(bo, user);

        user.setPassword(Md5Util.getMd5(bo.getPassword()));
        user.setGender((byte) 0);
        user.setUserLevel((byte) 0);
        user.setNickname(bo.getUsername());
        user.setAvatar("http://182.92.235.201:8083/wx/storage/fetch/45srjkbyvfo3utgxxfdo.gif");
        user.setWeixinOpenid(bo.getWxCode());
        user.setStatus((byte) 0);
        user.setAddTime(date);
        user.setUpdateTime(date);
        user.setDeleted(false);
        userMapper.insertSelective(user);
        //物理删除数据库信息
//        codeMapper.deleteByExample(codeExample);
        return 0;
    }

}
